package biz

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"time"

	"github.com/go-kratos/kratos/v2/log"
)

type CustomerUsecase struct {
	repo CustomerData
	log  *log.Helper
}

func NewCustomerUsecase(repo CustomerData, logger log.Logger) *CustomerUsecase {
	return &CustomerUsecase{repo: repo, log: log.NewHelper(logger)}
}

type CustomerData interface {
	SetVerifyCode(ctx context.Context, phone string, code string, ex int) (err error)
}

// 核心业务逻辑
func (uc *CustomerUsecase) GenerateVerifyCode(ctx context.Context, phone string) (string, error) {
	rand.Seed(time.Now().UnixNano())
	num := rand.Intn(899999) + 100000 // 生成 100000~999999 的六位数
	code := strconv.Itoa(num)         // 转成字符串
	err := uc.repo.SetVerifyCode(ctx, phone, code, 60)
	return fmt.Sprintf("%d", code), err
}
